package org.example.struct.array;

import com.alibaba.fastjson.JSON;

/**
 * @author liyishan
 * @date 2024/8/29 10:35
 * @apiNote
 */

public class Test00832FlipAndInvertImage {
    public static void main(String[] args) {
        int[][] image = {{1, 1, 0}, {1, 0, 1}, {0, 0, 0}};
        int[][] res = flipAndInvertImage(image);
        System.out.println(JSON.toJSONString(res));
    }

    public static int[][] flipAndInvertImage(int[][] image) {
        int m = image.length;
        int n = image[0].length;
        for (int i = 0; i < m; i++) {
            swap(image[i]);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                image[i][j] = 1 - image[i][j];
            }
        }
        return image;
    }

    private static void swap(int[] image) {
        int left = 0;
        int right = image.length - 1;
        while (left < right) {
            int temp = image[left];
            image[left] = image[right];
            image[right] = temp;
            left++;
            right--;
        }
    }

    public int[][] flipAndInvertImage1(int[][] image) {
        int n = image.length;
        for (int i = 0; i < n; i++) {
            int left = 0, right = n - 1;
            while (left < right) {
                if (image[i][left] == image[i][right]) {
                    image[i][left] ^= 1;
                    image[i][right] ^= 1;
                }
                left++;
                right--;
            }
            if (left == right) {
                image[i][left] ^= 1;
            }
        }
        return image;
    }
}
